package leetcode.editor.week.week307;

// 2383. 赢得比赛需要的最少训练时长
// https://leetcode.cn/contest/weekly-contest-307/problems/minimum-hours-of-training-to-win-a-competition/
public class Solution2383 {
    public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
        int n = energy.length;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            // 精力上无法打败对手，因此最开始需要增加一些精力，使得精力恰好满足要求
            if (initialEnergy <= energy[i]) {
                int t = energy[i] - initialEnergy + 1;
                ans += t;
                initialEnergy += t;
            }
            // 经历减去
            initialEnergy -= energy[i];

            // 经验上无法打败对手，因此最开始需要增加一些经验，使得经验恰好满足要求
            if (initialExperience <= experience[i]) {
                int t = experience[i] - initialExperience + 1;
                ans += t;
                initialExperience += t;
            }

            // 经验加上
            initialExperience += experience[i];
        }

        return ans;
    }
}
